package com.yunhe.abnormal.service.data.impl;

import com.yunhe.abnormal.client.DeviceClient;
import com.yunhe.abnormal.client.MeasRealtimeClient;
import com.yunhe.abnormal.formula.FormulaFunc;
import com.yunhe.common.model.device.Analog;
import com.yunhe.abnormal.service.data.TaskService;
import com.yunhe.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class TaskServiceImpl implements TaskService {

    private static final Pattern p = Pattern.compile("var\\([^\\)]*\\)");

    @Autowired
    private MeasRealtimeClient measRealtimeClient;
    @Autowired
    private DeviceClient deviceClient;
    @Autowired
    private FormulaFunc formulaFunc;

    @Override
    public Double getValue(Long systemId, String formula) throws Exception {
        String str = formula.substring(formula.indexOf("var(") + 4, formula.length() - 1);
        if (StringUtil.isNumber(str)) {
            Long pn = Long.parseLong(str);
            Map<Long, Double> map = measRealtimeClient.getMeasureRealtime(Arrays.asList(pn)).getBody().getResults();
            if (map != null) {
                return map.get(pn);
            } else {
                return 0D;
            }
        } else {
            return (Double) formulaFunc.func(str);
        }
    }

    @Override
    public String getAlarmValue(Long systemId, String formula) throws Exception {
        Matcher matcher = p.matcher(formula);
        Set<String> result = new HashSet<>();
        while (matcher.find()) {
            result.add(matcher.group());
        }
        if (!formula.contains("strategyPower")) {
            result.remove("var(10001000310014435)");
        }
        StringBuilder alarmValue = new StringBuilder();
        for (String s : result) {
            String str = s.substring(s.indexOf("var(") + 4, s.length() - 1);
            if (StringUtil.isNumber(str)) {
                //点号
                Long pn = Long.parseLong(str);
                //点号值
                Double value = getValue(systemId, s);
                //获取点号analog
                Analog analogs = deviceClient.getAnalogByPointNumber(pn).getBody().getResults();
                if (analogs != null) {
                    alarmValue.append(analogs.getTitle())
                            .append(":").append(value).append(",");
                }
            }
        }
        String string = alarmValue.toString();
        if (string.length() > 0) {
            return string.substring(0, string.length() - 1);
        } else {
            return string;
        }
    }

//    @Override
//    public List<Map<String, Dev>> getAllDevsByFirm(Long systemId, List<Map<String, String>> configList) throws Exception {
//        List<Map<String, Dev>> rList = new ArrayList<>();
//        List<Long> devIdList = new ArrayList<>();
//        if (redisClient.get(("warn" + systemId).getBytes()) == null) {
//            List<Dev> devList = (List<Dev>)feignApiClient.getDevsByFirm(systemId, systemId).getBody().getResults();
//            for (Dev dev : devList) {
//                devIdList.add(dev.getId());
//            }
//            List<Analog> analogs = (List<Analog>)feignApiClient.findAnalogsByDevId(systemId, devIdList).getBody().getResults();
//            List<String> strList = new ArrayList<>();
//            for (Map<String, String> map : configList) {
//                for (Map.Entry<String, String> entry : map.entrySet()) {
//                    for (Analog analog : analogs) {
//                        if (analog.getTypeCode() == Long.parseLong(entry.getValue())) {
//                            strList.add(entry.getKey() + "," + analog.getPointNumber() + "#" + analog.getDevCode());
//                        }
//                    }
//                }
//            }
//            Map<String, Dev> rMap;
//            for (Dev dev : devList) {
//                rMap = new LinkedHashMap<>();
//                for (String str : strList) {
//                    String[] s = str.split("#");
//                    if (Long.parseLong(s[1]) == dev.getCode()) {
//                        rMap.put(s[0], dev);
//                        rList.add(rMap);
//                    }
//                }
//            }
//            redisClient.set(("warn" + systemId).getBytes(), ObjectAndByteUtil.toByteArray(rList), 86400);
//        } else {
//            rList = (List<Map<String, Dev>>)ObjectAndByteUtil.toObject(redisClient.get(("warn" + systemId).getBytes()));
//        }
//        Set<Map<String, Dev>> set = new HashSet<>(rList);
//        return new ArrayList<>(set);
//    }
}
